package com.hj.security.config;

import cn.hutool.core.util.ReUtil;
import com.hj.security.domain.annotation.NoAuth;
import jakarta.annotation.Resource;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;

/**
 * @author hj
 * @Date 2023/4/26
 * @Description: 不需要拦截的url
 */
@Slf4j
@Configuration
@ConditionalOnExpression("!'${security.oauth2.client.ignore-urls}'.isEmpty()")
@ConfigurationProperties(prefix = "security.oauth2.client")
public class PermitUrlProperties implements InitializingBean {

	private static final Pattern PATTERN = Pattern.compile("\\{(.*?)}");

	@Resource
	private WebApplicationContext applicationContext;

	@Getter
	@Setter
	private List<String> ignoreUrls = new ArrayList<>();

	@Override
	public void afterPropertiesSet() {
		RequestMappingHandlerMapping mapping = applicationContext.getBean(RequestMappingHandlerMapping.class);
		Map<RequestMappingInfo, HandlerMethod> map = mapping.getHandlerMethods();

		map.keySet().forEach(info -> {
			HandlerMethod handlerMethod = map.get(info);

			// 获取方法上边的注解 替代path variable 为 *
			NoAuth method = AnnotationUtils.findAnnotation(handlerMethod.getMethod(), NoAuth.class);
			Optional.ofNullable(method)
					.ifPresent(noAuth -> {
						assert info.getPatternsCondition() != null;
						info.getPatternsCondition().getPatterns()
								.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN, "*")));
					});

			// 获取类上边的注解, 替代path variable 为 *
			NoAuth controller = AnnotationUtils.findAnnotation(handlerMethod.getBeanType(), NoAuth.class);
			Optional.ofNullable(controller)
					.ifPresent(noAuth -> {
						assert info.getPatternsCondition() != null;
						info.getPatternsCondition().getPatterns()
								.forEach(url -> ignoreUrls.add(ReUtil.replaceAll(url, PATTERN,"*")));
					});
		});

	}




}
